import {SignalsRegister} from "@/utils/signals/signalRegister";

interface SignalMethods<T = any> {
	add(
		signalName: string,
		listener: (...params: T[]) => void,
		listenerContext?: any,
		priority?: Number
	): void;
	addOnce(
		signalName: string,
		listener: (...params: T[]) => void,
		listenerContext?: any,
		priority?: Number
	): void;
	dispatch(signalName: string, ...params: T[]): void;
	remove(signalName: string, listener: (...params: T[]) => void, context?: any): void;
	removeAll(signalName: string): void;
	setActive(signalName: string, active: boolean): void;
	halt(signalName: string): void;
	dispose(signalName: string): void;
	has(signalName: string, listener: (...params: T[]) => void, context?: any): boolean;
}

export function useSignal() {
	const add = (
		signalName,
		listener?,
		listenerContext?,
		priority?
	) => {
		SignalsRegister[signalName].add(listener, listenerContext, priority);
	};

	const addOnce = (
		signalName,
		listener,
		listenerContext,
		priority
	) => {
		SignalsRegister[signalName].addOnce(listener, listenerContext, priority);
	};

	const dispatch = (signalName, ...params) => {
		SignalsRegister[signalName].dispatch(...params);
	};

	const remove = (
		signalName,
		listener,
		context?
	) => {
		SignalsRegister[signalName].remove(listener, context);
	};

	const removeAll = (signalName) => {
		SignalsRegister[signalName].removeAll();
	};

	const setActive = (signalName, active) => {
		SignalsRegister[signalName].active = active;
	};

	const halt = (signalName) => {
		SignalsRegister[signalName].halt();
	};

	const dispose = (signalName) => {
		SignalsRegister[signalName].dispose();
	};

	const has = (
		signalName,
		listener,
		context?
	) => {
		return SignalsRegister[signalName].has(listener, context);
	};

	return { add, addOnce, dispatch, remove, removeAll, setActive, halt, dispose, has };
}

export function useAddSignal(
	signalName,
	listener,
	listenerContext?,
	priority?
) {
	SignalsRegister[signalName].add(listener, listenerContext, priority);
}

export function useDispatchSignal(signalName, ...arg) {
	// console.log("useDispatchSignal signalName",signalName);
	SignalsRegister[signalName].dispatch(...arg);
}

export function useRemoveSignal(signalName, listener) {
	SignalsRegister[signalName].remove(listener);
}

